﻿#include <iostream>
#include "CWinHttpClient.h"

#define UPDATE_URL1          _T("https://gitee.com/flame_cyclone/fc_font_tool/raw/master/Release/update.json")
#define UPDATE_URL2          _T("https://gitee.com/flame_cyclone/fc_font_tool/releases/download/1.0.0.4/FC_Font_Tool.exe")
#define TEST_NVIDIA_DRIVER   _T("https://cn.download.nvidia.com/Windows/561.09/561.09-desktop-win10-win11-64bit-international-dch-whql.exe")
#define TEST_AMD_DRIVER      _T("https://drivers.amd.com/drivers/whql-amd-software-adrenalin-edition-24.8.1-win10-win11-aug-rdna.exe")
#define TEST_BILIBILI_DM     _T("https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=4412054")
#define TEST_WEPE_URL        _T("https://mirrors.lzu.edu.cn/wepe/WePE_64_V2.3.exe")


int main()
{
    CWinHttpClient obj;

    CWinHttpValue header = CWinHttpObject{
        {_T("User-Agent"), _T("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0")},
        {_T("Content-Type"), _T("application/json;charset=UTF-8")},
        {_T("Accept"), _T("*/*")},
        {_T("Referer"), _T("https://www.amd.com/")},
    };

    CWinHttpValue param = CWinHttpObject{
        {_T("Accept"), _T("application/x-clarity-gzip")},
        {_T("Accept-Encoding"), CWinHttpArray {_T("gzip"), _T("deflate"), _T("br"), _T("zstd")}},
        {_T("Accept-Language"), CWinHttpArray {_T("zh-CN"), _T("zh;q=0.9"), _T("en;q=0.8"), _T("en-GB;q=0.7"), _T("en-US;q=0.6")}},
    };

    _tstring strGet = param.AsGetString();
    _tstring strJsonParam = param.AsJsonString();
    _tstring strHeader = param.AsHeaderString();

    CWinHttpResult get = obj.Get(_T(R"(https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=4412054)"), {}, header, nullptr);

    CWinHttpResult post = obj.Post("https://ayaneo.com.cn/backend/ios/pc-space/user-upload-device?app=true", R"({"deviceId":"AN1OSCH012123000262C","deviceName":"AYANEO 2021","language":"zh-CN","pcid":"","powerCustomMaxValue":1,"powerCustomMinValue":1,"token":"cd5340ea63af1c2b376f36424f607810c186e03e","vibrationCustomMaxValue":1,"vibrationCustomMinValue":1})");

    post.result = obj.DecoderFromUtf8(post.result);

    std::string  strUtf8 = post.result;

    std::string strResult = CWinHttpClient::DecoderFromUtf8(R"({"string":"\uD83C\uDF0D\u6211\u662F\u5730\u7403"})");

    bool fResult;
    clock_t startTime = ::clock();

    CWinHttpResult downResult = obj.DownloadFile(TEST_AMD_DRIVER, _T(""), header.AsHeaderString(), [](const WINHTTP_PROGRESS_INFO& progress) {
        CWinHttpClient::ConsoleOutput(_T("%d/%d Time: %.3lfs Progress: %.3lf%% %.1lfMB/%.1lfMB Speed: %.1lf Mbps %.1lfMB/s RemainTime: %.1lfs\n"),
        progress.nActiveThread, progress.nTotalThread,
        (double)progress.costTime / 1000.0f,
        progress.lfProgress * 100, 
        (double)progress.ullCur / (1024.0f * 1024.0f), (double)progress.ullTotal / (1024.0f * 1024.0f),
        progress.lfSpeed / (1024.0f * 1024.0f) * 8.0f,
        progress.lfSpeed / (1024.0f * 1024.0f),
        progress.lfRemainTime
        );

    return true;
        }, 2
    );

    clock_t endTime = ::clock();

    CWinHttpClient::ConsoleOutput(_T("Cost Time: %dms\r\n"), endTime - startTime);

    return 0;
}
